package util;


import javax.xml.transform.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {
    /**
     * 重写文件
     *
     * @param content
     * @param filepath
     * @param filename
     */
    public static void reWriteFile(String content, String filepath, String filename) {
        writeToFile(content, filepath, filename, false);
    }

    /**
     * 追加内容
     *
     * @param content
     * @param filepath
     * @param filename
     */
    public static void addToFile(String content, String filepath, String filename) {
        writeToFile(content, filepath, filename, true);
    }

    public static boolean deleteFile(File file) {
        try {
            return file.delete();
        } catch (Exception e) {
            return false;
        }
    }


    public static void write(Source source, Result result) {
        TransformerFactory tffactory = TransformerFactory.newInstance();
        Transformer tr;
        try {
            tr = tffactory.newTransformer();
            tr.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public static String readFileToString(String filePath) {
        try {
            FileInputStream in = new FileInputStream(filePath);
            String content = new String(readStream(in), "utf-8");
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outSteam.close();
            inStream.close();
        }
        return outSteam.toByteArray();
    }

    public static List<String> readLines(String filePath) {
        List<String> result = new ArrayList<>();
        try {
            FileReader reader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                result.add(line);
            }
            reader.close();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static void writeToFile(String content, String filepath, String filename, Boolean append) {
        try {
            File floder = new File(filepath);
            if (!floder.exists()) {
                floder.mkdirs();
            }
            File file = new File(filepath + "/" + filename);
            if (!file.exists()) {
                file.createNewFile();
            }
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file, append), "utf-8");
            out.write(content);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
